/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 *
 */

package com.lehe.service.impl;

import com.lehe.common.exceptions.LHAdminException;
import com.lehe.common.utils.StringUtil;
import com.lehe.dao.AdminUserMappingDao;
import com.lehe.entry.AdminUserMappingDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 类的相关描述
 *
 * @author 王丹
 * @since 2017-03-09
 */
@Transactional
@Service
public class AdminUserMappingService extends AbstractService {

    @Autowired
    AdminUserMappingDao adminUserMappingDao;

    public String queryByAdminUserId(Long adminUserId){
        String ret = "";
        List<AdminUserMappingDto> adminUserMappingDtos = adminUserMappingDao.queryByAdminUserId(adminUserId);
        if(null!=adminUserMappingDtos && !adminUserMappingDtos.isEmpty()){
            ret = StringUtil.join(adminUserMappingDtos, ",");
        }
        return ret;
    }

    public void insertAdminUserMapping(AdminUserMappingDto ... adminUserMappingDtos) throws LHAdminException {
        for(AdminUserMappingDto adminUserMappingDto : adminUserMappingDtos){
            AdminUserMappingDto existUser = adminUserMappingDao.queryByUserId(adminUserMappingDto.getUserId());
            if(null!=existUser){
                throw new LHAdminException("UserID" + existUser.getUserId() + "已经存在");
            }
            adminUserMappingDao.insertAdminUserMapping(adminUserMappingDto);
        }
    }

    public Integer deleteByAdminUserId(Long adminUserId){
        return adminUserMappingDao.deleteByAdminUserId(adminUserId);
    }

	public Integer deleteByUserId(Long userId) {
		return adminUserMappingDao.deleteByUserId(userId);
	}
}
